package net.jumpengine.event;

import java.util.HashMap;
import java.util.Map;

import javafx.scene.input.MouseButton;
import net.jumpengine.Manager;
import net.jumpengine.bean.Base;
import net.jumpengine.inter.IClick;
import net.jumpengine.inter.IControlEvent;
import net.jumpengine.inter.IMouseEvent;

public class MouseMiddle {
	private Map<Enum<?>,IMouseEvent> map=new HashMap<>();
	private Map<Enum<?>,Boolean> first=new HashMap<>();
	private int cx,cy;
	
	public void click(int x,int y,MouseButton button) {
		IMouseEvent mouseEvent=map.get(Manager.getState());
		
		IClick click = Manager.getBaseManager().FindBaseByPoint(x, y,IClick.class);
		
		if(click!=null) {
			if(click instanceof IControlEvent) {
				IControlEvent event=(IControlEvent) click;
				if(event.getMouseEvent()!=null)event.getMouseEvent().click(x, y, button);
			}
			if(click.getAction()!=null)click.getAction().handle(x, y, button, click);
		}else {
			if(mouseEvent==null) {
				if(!first.containsKey(Manager.getState())) {
					Manager.getLog().error(MouseMiddle.class,"[click] state:", Manager.getState()," mouseEvent为null");
					first.put(Manager.getState(), false);
				}
				return;
			}
			mouseEvent.click(x, y,button);
		}
	}
	
	public void move(int x,int y,MouseButton button) {
		if(cx==0) {
			cx=x;
			cy=y;
		}else {
			if(absMath(x,y,cx,cy)<2) {
				return;				
			}
			cx=x;
			cy=y;
		}
		IMouseEvent mouseEvent=map.get(Manager.getState());
		if(mouseEvent==null) {
			if(!first.containsKey(Manager.getState())) {
				Manager.getLog().error(MouseMiddle.class,"[move] state:", Manager.getState()," mouseEvent为null");
				first.put(Manager.getState(), false);
			}
			return;
		}
		Base base = Manager.getBaseManager().FindBaseByPoint(x, y,Base.class);
		if(base !=null && base instanceof IControlEvent) {
			IControlEvent event=(IControlEvent) base;
			if(event.getMouseEvent()!=null)event.getMouseEvent().mouseMove(x, y, button);
		}
		mouseEvent.mouseMove(x, y,button);
	}
	
	public void pressed(int x, int y, MouseButton button) {
		IMouseEvent mouseEvent=map.get(Manager.getState());
		if(mouseEvent==null) {
			if(!first.containsKey(Manager.getState())) {
				Manager.getLog().error(MouseMiddle.class,"[pressed] state:", Manager.getState()," mouseEvent为null");
				first.put(Manager.getState(), false);
			}
			return;
		}
		Base base = Manager.getBaseManager().FindBaseByPoint(x, y,Base.class);
		if(base !=null && base instanceof IControlEvent) {
			IControlEvent event=(IControlEvent) base;
			if(event.getMouseEvent()!=null)event.getMouseEvent().mousePressed(x, y, button);
		}
		mouseEvent.mousePressed(x, y,button);
	}
	
	private int absMath(int x1,int y1,int x2,int y2) {
		return (x1<x2?x2-x1:x1-x2)+(y1<y2?y2-y1:y1-y2);
	}

	public void released(int x, int y, MouseButton button) {
		IMouseEvent mouseEvent=map.get(Manager.getState());
		if(mouseEvent==null) {
			if(!first.containsKey(Manager.getState())) {
				Manager.getLog().error(MouseMiddle.class,"[released] state:", Manager.getState()," mouseEvent为null");
				first.put(Manager.getState(), false);
			}
			return;
		}
		Base base = Manager.getBaseManager().FindBaseByPoint(x, y,Base.class);
		if(base !=null && base instanceof IControlEvent) {
			IControlEvent event=(IControlEvent) base;
			if(event.getMouseEvent()!=null)event.getMouseEvent().mouseReleased(x, y, button);
		}
		mouseEvent.mouseReleased(x, y,button);
	}
	
	public void put(Enum<?> e,IMouseEvent event) {
		map.put(e, event);
	}
	
	public IMouseEvent get(Enum<?> e) {
		return map.get(e);
	}
}
